class_name TextHandler

extends Control

const SECTION_NAME = "RollingText"

@export_range(0, 0.02, 0.001) var roll_speed: float = 0.003
@export_range(0, 1) var font_size_percent: float = 0.8
@export var spacing: float = 0.2

@onready var label: Label = %Label1

var seq1: Label
var seq2: Label

var label_params: LabelSettings

var current_pixel_speed = null
var prev_text: Array[String]

var text_color: Color:
	get:
		return label_params.font_color

var text: String:
	get:
		return label.text

func _ready() -> void:
	roll_speed = ConfigManager.config.get_value(SECTION_NAME, "roll_speed", 0.003)
	font_size_percent = ConfigManager.config.get_value(SECTION_NAME, "size_ratio", 0.8)
	spacing = ConfigManager.config.get_value(SECTION_NAME, "spacing_ratio", 0.2)
	label.text = ConfigManager.config.get_value(SECTION_NAME, "prev_text", ["Neon Light"])[0]
	
	label_params = LabelSettings.new()
	label_params.font_color = ConfigManager.config.get_value(SECTION_NAME, "color", Color.WHITE)
	label_params.font_size = get_viewport_rect().size.y * font_size_percent
	label.label_settings = label_params
	call_deferred(&"check_rolling")


func _process(delta: float) -> void:
	if current_pixel_speed == null:
		return
	
	var screen_width = get_viewport_rect().size.x
	if seq2.position.x + seq2.get_rect().size.x < (1.0 - spacing) * screen_width:
		var t = seq1
		seq1 = seq2
		seq2 = t
		seq2.position.x = seq1.position.x + seq1.get_rect().size.x + spacing * screen_width 
	
	seq1.position.x -= current_pixel_speed
	seq2.position.x -= current_pixel_speed


func set_roll_speed(speed: float) -> void:
	roll_speed = speed
	if current_pixel_speed != null:
		current_pixel_speed = get_viewport_rect().size.x * roll_speed
		seq1.position.x = 0
		seq2.position.x = seq1.get_rect().size.x + spacing * get_viewport_rect().size.x


func set_spacing(percent: float) -> void:
	spacing = percent
	call_deferred(&"check_rolling")


func set_text(text: String) -> void:
	label.text = text
	reset_label_layout()
	call_deferred(&"check_rolling")


func set_text_color(color: Color) -> void:
	label_params.font_color = color
	if seq1 != null and seq2 != null:
		seq1.label_settings = label_params
		seq2.label_settings = label_params
	else:
		label.label_settings = label_params


func set_text_percent(percent: float) -> void:
	font_size_percent = percent
	label_params.font_size = get_viewport_rect().size.y * font_size_percent
	label.label_settings = label_params
	
	reset_label_layout()
	call_deferred(&"check_rolling")


func reset_label_layout() -> void:
	label.size = label.get_minimum_size()
	label.anchor_top = 0
	label.anchor_bottom = 1
	label.offset_top = 0
	label.offset_bottom = 0


func check_rolling() -> void:
	var text_length = label.get_rect().size.x
	var screen_width = get_viewport_rect().size.x
	label.position.x = 0
	if not label.text.is_empty() and text_length > screen_width:
		current_pixel_speed = screen_width * roll_speed
		if seq1 != null and seq1 != label:
			seq1.queue_free()
			seq1 = null
		if seq2 != null and seq2 != label:
			seq2.queue_free()
			seq2 = null
		
		seq1 = label
		seq2 = label.duplicate()
		seq2.position.x = seq1.get_rect().size.x + spacing * screen_width
		add_child(seq2)
	else:
		current_pixel_speed = null
		
		label.set_anchors_preset(Control.PRESET_CENTER)
		label.offset_bottom = 0
		label.offset_left = 0
		label.offset_right = 0
		label.offset_top = 0
		
		if seq1 != null and seq1 != label:
			seq1.queue_free()
			seq1 = null
		if seq2 != null and seq2 != label:
			seq2.queue_free()
			seq2 = null
